package com.demo2do.fms.admin.entity.search.supplier;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.demo2do.fms.entity.supplier.common.AuditStatus;
import com.demo2do.fms.entity.supplier.common.BusinessNature;
import com.demo2do.fms.entity.supplier.common.Type;

/**
 * 
 * @author mohanlan
 *
 */
public class SubSupplierSearch implements QueryBuilder {
    
	/** 运输公司代码 */
	private String code;
	
	/** 运输公司名称 */
	private String name;
    
    /** 分区code */
    private String branch;
    
    /** 供方类型 */
    private Type type;
    
    /** 业务性质 */
    private BusinessNature businessNature;
    
    /** 供方审核状态 */
    private AuditStatus auditStatus;
    
    /** 业务类型 */
    private List<String> businessTypes;
    
    private QueryFilter filter;
    
    private QueryOrder order;
    
    /**
     * The default constructor
     */
    public SubSupplierSearch() {
        order = new QueryOrder().add("lastModifyTime", "DESC");
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
    	
        if (filter == null || refresh) {
        	
            filter = new QueryFilter().addLike("code", code)
				            		  .addLike("name", name)
                                      .addEquals("branch.code", branch)
                                      .addEquals("type", type)
                                      .addEquals("status", auditStatus)
                                      .addEquals("businessNature", businessNature);
            
            if (CollectionUtils.isNotEmpty(businessTypes)) {
                String businessTypeString = StringUtils.join(businessTypes, "%");
                filter.addLike("businessType", businessTypeString);
            }
            
        }
        
        return filter;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "FROM Supplier Where disabled = false" + getPreparedFilter(false).clauseSentence() + order.sentence();
        return sentence;
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /**
     * @param supplierIds
     */
    public void addSupplierIds(List<Long> supplierIds) {
        supplierIds.add(0L);
    	this.getPreparedFilter(false).addSentence("id IN (:ids)", "ids", supplierIds);
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return order;
    }
    
    /**
     * @return
     */
    public QueryFilter getFilter(){
        return getPreparedFilter(false);
    }

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the branch
	 */
	public String getBranch() {
		return branch;
	}

	/**
	 * @return the type
	 */
	public Type getType() {
		return type;
	}
	
	/**
	 * @return
	 */
	public BusinessNature getBusinessNature() {
        return businessNature;
    }

	/**
	 * @return the auditStatus
	 */
	public AuditStatus getAuditStatus() {
		return auditStatus;
	}
	
	/**
	 * @return
	 */
	public List<String> getBusinessTypes() {
        return businessTypes;
    }

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @param branch the branch to set
	 */
	public void setBranch(String branch) {
		this.branch = branch;
	}

	/**
	 * @param type the type to set
	 */
	public void setType(Type type) {
		this.type = type;
	}
	
	/**
	 * @param businessNature
	 */
	public void setBusinessNature(BusinessNature businessNature) {
        this.businessNature = businessNature;
    }

	/**
	 * @param auditStatus the auditStatus to set
	 */
	public void setAuditStatus(AuditStatus auditStatus) {
		this.auditStatus = auditStatus;
	}
	
	/**
	 * @param businessTypes
	 */
	public void setBusinessTypes(List<String> businessTypes) {
        this.businessTypes = businessTypes;
    }
    
}
